package dtu.app.fragment;

import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.iqengines.demo.R;

import dtu.app.listadapter.CategoryArrayAdapter;
import dtu.app.listloader.CategoryListLoader;
import dtu.app.model.Category;

public class CategoryListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Category>> {

	CategoryArrayAdapter mAdapter;
	View previousView;
	OnCategoryItemSelectedListener mListener;
	boolean firstClick = true;

	public interface OnCategoryItemSelectedListener {
	    public void onCategoryItemSelected(String category);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (OnCategoryItemSelectedListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString() + " must implement OnCategoryItemSelectedListener");
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if (mAdapter != null) {
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		System.out.println("DataListFragment.onActivityCreated");

		// Initially there is no data 
		setEmptyText("No Data Here");

		// Create an empty adapter we will use to display the loaded data.
		mAdapter = new CategoryArrayAdapter(getActivity());
		setListAdapter(mAdapter);

		// Start out with a progress indicator.
		setListShown(false);

		// Prepare the loader.  Either re-connect with an existing one,
		// or start a new one.
		getLoaderManager().initLoader(0, null, this);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// Insert desired behavior here.
		
		if (firstClick) {
			firstClick = false;
			((View) getListView().getChildAt(0)).setBackgroundColor(Color.TRANSPARENT);
		}
		
		if (previousView != null) {
			previousView.setBackgroundColor(Color.TRANSPARENT);
		}
		previousView = v;
		v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
		
		// TODO: CREATE CATEGORY ENUM INSTEAD AND PARSE IT INSTEAD
		String category = (String) ((TextView)v.findViewById(R.id.category_name)).getText();
		mListener.onCategoryItemSelected(category);
		
		Log.i("DataListFragment", "Item clicked: " + id);
		
	}
	

	@Override
	public Loader<List<Category>> onCreateLoader(int arg0, Bundle arg1) {
		System.out.println("DataListFragment.onCreateLoader");
		return new CategoryListLoader(getActivity());
	}

	@Override
	public void onLoadFinished(Loader<List<Category>> arg0, List<Category> data) {
		mAdapter.setData(data);
		System.out.println("DataListFragment.onLoadFinished");
		// The list should now be shown.
		if (isResumed()) {
			setListShown(true);
		} else {
			setListShownNoAnimation(true);
		}
	}

	@Override
	public void onLoaderReset(Loader<List<Category>> arg0) {
		mAdapter.setData(null);
	}
	
	public void updateList() {	
		Log.i("DataListFragment", "updateList");
		getLoaderManager().restartLoader(0, null, this);
	}
}